package com.example.a2022application.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;


import com.example.a2022application.BaseActivity;
import com.example.a2022application.MainActivity;
import com.example.a2022application.R;
import com.example.a2022application.bean.RegisterBean;
import com.example.a2022application.bean.TeacherBean;
import com.example.a2022application.bean.UserTimetableBean;
import com.example.a2022application.login_administrator;
import com.example.a2022application.net.Urls;
import com.example.a2022application.utils.SpUtils;
import com.example.a2022application.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import androidx.annotation.Nullable;

/**
 * 添加授课老师
 */
public class AddTeacherActivity extends BaseActivity implements View.OnClickListener {
    private ListView listteacher;
    ShowListAdapter adapter;

    List tList= new ArrayList();
    String uid = "";
    private TextView tv_tea,tv_del;
    private String objid;
    private String classname;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addteacher);
        listteacher = findViewById(R.id.listteacher);
        tv_tea = findViewById(R.id.tv_tea);
        tv_del = findViewById(R.id.tv_del);
        tv_del.setOnClickListener(this);
        Intent intent = getIntent();
        uid = intent.getStringExtra("oid");
        objid = intent.getStringExtra("objid");
        classname = intent.getStringExtra("classname");

        initData();
        initTeacher();

    }

    private void initTeacher() {
        OkGo.<String>post(Urls.SELECTTEACHER)
                .params("timetableId",uid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        UserTimetableBean bean = getGson().fromJson(response.body(),UserTimetableBean.class);
                        if (bean.getCode() == 0){
                            if (bean.getData().size()>0){
                                String name = bean.getData().get(0).getNickname();
                                tv_tea.setText("当前授课老师："+name);
                                tv_del.setVisibility(View.VISIBLE);
                            }else {
                                tv_tea.setText("当前授课老师：");
                                tv_del.setVisibility(View.GONE);
                            }

                        }

                    }
                });
    }

    private void initData() {
        OkGo.<String>post(Urls.TEACHERSLIST)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        TeacherBean bean = getGson().fromJson(response.body(),TeacherBean.class);
                        Log.e("TAG","老师---"+response.body());
                        if (bean.getCode() == 0){
                            if (bean.getData().size()>0){
                                tList = bean.getData();
                                adapter = new ShowListAdapter();
                                listteacher.setAdapter(adapter);
                                listteacher.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                        TeacherBean.DataBean teacher = (TeacherBean.DataBean) tList.get(i);
                                        new AlertDialog.Builder(AddTeacherActivity.this)
                                                .setTitle("确定设置授课老师吗？")
                                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(DialogInterface dialogInterface, int i) {
                                                        setTeacher(teacher.getObj_id());
                                                    }

                                                })
                                                .setNegativeButton("取消", null)
                                                .show();
                                    }
                                });
                            }
                        }
                    }
                });
    }

    private void setTeacher(String obj_id) {
        OkGo.<String>post(Urls.ADDTEACHER)
                .params("userId",obj_id)
                .params("timetableId",uid)
                .params("classname",classname)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.e("TAG","设置授课老师---"+response.body());
                        RegisterBean bean = getGson().fromJson(response.body(),RegisterBean.class);
                        if (bean.getCode() == 0){
                            initTeacher();
                        }
                        ToastUtil.showToast(bean.getMessage());

                    }
                });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_del:
                new AlertDialog.Builder(AddTeacherActivity.this)
                        .setTitle("确定删除授课老师吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                delTeacher();//删除授课老师
                            }

                        })
                        .setNegativeButton("取消", null)
                        .show();

                break;
        }
    }

    private void delTeacher() {
        OkGo.<String>post(Urls.DELTEACHER)
                .params("timetableId",uid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        RegisterBean bean = getGson().fromJson(response.body(),RegisterBean.class);
                        if (bean.getCode() == 0){
                            initTeacher();
                        }
                        ToastUtil.showToast(bean.getMessage());
                    }
                });

    }

    public class ShowListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return tList.size();
        }

        @Override
        public Object getItem(int i) {
            return tList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ShowListAdapter.ViewHolder viewHolder;
            //判断界面是否有缓存
            if(convertView == null){
                viewHolder= new ShowListAdapter.ViewHolder();
                convertView= LinearLayout.inflate(parent.getContext(), R.layout.item_dropdown, null);

                viewHolder.tv_name = convertView.findViewById(R.id.tv_name);

                convertView.setTag(viewHolder);
            }else{
                viewHolder=(ShowListAdapter.ViewHolder)convertView.getTag();
            }
            TeacherBean.DataBean teacher = (TeacherBean.DataBean) tList.get(position);

            viewHolder.tv_name.setText(teacher.getNickname());

            return convertView;
        }

        private class ViewHolder{
            TextView tv_name;

        }
    }

    //返回
    public void back(View view){
        finish();
    }
}
